package com.wzutils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Hu on 2017/3/17.
 */

public class TimeUtils {
    public static String YEAR_MONTH_DAY_HOUR_MIN_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * String 转date类型
     *
     * @param str
     * @param dateType
     * @return
     */
    public static Date stringToDate(String str, String dateType) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date 格式化成string
     *
     * @param date
     * @param dateType
     * @return
     */
    public static String dateToString(Date date, String dateType) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        String dateString = sdf.format(date);
        return dateString;
    }

    /**
     * 将long类型转化为Date
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date longToDate(long str) throws ParseException {
        return new Date(str * 1000);
    }

    /**
     * @param time
     * @param dateType
     * @return
     */
    public static String longToString(long time, String dateType) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        Date date = new Date(time * 1000);
        String str = sdf.format(date);
        return str;
    }

    public static long systemTimeLong() {
        return System.currentTimeMillis();
    }

    public static long stringTolong(String str, String formatType) {
        Date date = stringToDate(str, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // date类型转换为long类型
    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }
}
